package com.innovation.chengzhangriji.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.google.gson.JsonElement;
import com.innovation.chengzhangriji.Baby;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.database.DatabaseManager;
import com.innovation.chengzhangriji.http.Client;
import com.innovation.chengzhangriji.http.ResponseHandler;
import com.innovation.chengzhangriji.preferences.LoginInfo;
import com.innovation.chengzhangriji.ui.widget.CuteBabyAvatarView;
import com.innovation.chengzhangriji.ui.widget.CuteButton;
import com.innovation.chengzhangriji.ui.widget.CuteTextView;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class SuperTipDialogActivity extends BaseActivity implements View.OnClickListener {

    private int mBabyID;

    private String mApplyID;

    private int mInvitationID;

    private int mState;

    private DisplayImageOptions options; //配置图片加载及显示选项

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        Intent intent = getIntent();
        String title = null;
        mState = intent.getIntExtra("state", 0);
        String message = intent.getStringExtra("message");
        String text = null;
        switch (mState) {
            case 0:
                title = "申请确认";
                text = "允许加入";
                break;
            case 1:
                title = "受邀确认";
                text = "我要加入";
                break;
            case 2:
                title = "邀请批准";
                text = "马上邀请";
                break;
        }
        setContentView(R.layout.activity_super_tip_dialog);
        CuteTextView textTitle = (CuteTextView) findViewById(R.id.text_title);
        textTitle.setText(title);
        // RoundedImageView imageHead = (RoundedImageView) findViewById(R.id.image_head);
        CuteBabyAvatarView imageHead = (CuteBabyAvatarView) findViewById(R.id.image_head);
        CuteTextView textMessage = (CuteTextView) findViewById(R.id.text_message);
        textMessage.setText(message);
        CuteButton btnOK = (CuteButton) findViewById(R.id.button_ok);
        btnOK.setText(text);
        CuteButton btnCancel = (CuteButton) findViewById(R.id.button_cancel);
        btnOK.setOnClickListener(this);
        btnCancel.setOnClickListener(this);

        mBabyID = getIntent().getIntExtra("babyID", 0);
        String url = getIntent().getStringExtra("headUrl");
        setOptions();
        // ImageLoader.getInstance().displayImage(url, imageHead, options);
        imageHead.setAvatar(url);
        Baby baby = DatabaseManager.getInstance(this).queryBaby(mBabyID);
        if(baby != null) {
            imageHead.setSex(baby.getGender());
        } else {
            imageHead.setSex(getIntent().getIntExtra("sex", 0));
        }
        mApplyID = getIntent().getStringExtra("applyID");
        mInvitationID = getIntent().getIntExtra("invitationID", 0);
    }


    @Override
    public void onClick(View v) {
        boolean agree = false;
        switch (v.getId()) {
            case R.id.button_ok:
                agree = true;
                break;
            case R.id.button_cancel:
                agree = false;
                break;
        }
        switch (mState) {
            case 0:
                applyHandler(agree);
                break;
            case 1:
                normalHandler(agree);
                break;
            case 2:
                adminHandler(agree);
                break;
        }
    }

    // 审核
    private void applyHandler(boolean agree) {
        int state;
        if (agree) {
            state = 1;
        } else {
            state = 2;
        }
        LoginInfo loginInfo = Config.getLoginInfo(this);
        if (loginInfo == null) {
            return;
        }
        String phone = loginInfo.getPhone();
        String token = loginInfo.getToken().getAccessToken();
        Client.requestCheckJoin(this, phone, token, mBabyID, mApplyID, state, new ResponseHandler() {

            @Override
            public void onInnovationStart() {
                showProgressDialog(null, null);
            }

            @Override
            public void onInnovationSuccess(JsonElement value) {
                int success = value.getAsInt();
                if (success == 1) {
                    Intent returnIntent = new Intent();
                    setResult(RESULT_OK, returnIntent);
                    finish();
                }
            }

            @Override
            public void onChZhRJFailure(String value) {
                tipShow(value);
                Intent returnIntent = new Intent();
                setResult(RESULT_OK, returnIntent);
                finish();
            }

            @Override
            public void onInnovationFinish() {
                dismissProgressDialog();
            }

            @Override
            public void onInnovationExceptionFinish() {
                dismissProgressDialog();
            }

        }, networkUnavailableSampleImpl());
    }

    // 受邀人
    private void normalHandler(boolean agree) {
        int state;
        if (agree) {
            state = 2;
        } else {
            state = 3;
        }
        LoginInfo loginInfo = Config.getLoginInfo(this);
        if (loginInfo == null) {
            return;
        }
        String phone = loginInfo.getPhone();
        String token = loginInfo.getToken().getAccessToken();
        Client.requestInviteComplete(this, phone, token, mInvitationID, mBabyID, state, new ResponseHandler() {

            @Override
            public void onInnovationStart() {
                showProgressDialog(null, null);
            }

            @Override
            public void onInnovationSuccess(JsonElement value) {
                int success = value.getAsInt();
                if (success == 1) {
                    Intent returnIntent = new Intent();
                    setResult(RESULT_OK, returnIntent);
                    finish();
                }
            }

            @Override
            public void onChZhRJFailure(String value) {
                tipShow(value);
                Intent returnIntent = new Intent();
                setResult(RESULT_OK, returnIntent);
                finish();
            }

            @Override
            public void onInnovationFinish() {
                dismissProgressDialog();
            }

            @Override
            public void onInnovationExceptionFinish() {
                dismissProgressDialog();
            }
        }, networkUnavailableSampleImpl());
    }

    // 管理员批准
    private void adminHandler(boolean agree) {
        int state;
        if (agree) {
            state = 1;
        } else {
            state = 3;
        }
        LoginInfo loginInfo = Config.getLoginInfo(this);
        if (loginInfo == null) {
            return;
        }
        String phone = loginInfo.getPhone();
        String token = loginInfo.getToken().getAccessToken();
        Client.requestAdminInviteComplete(this, phone, token, mInvitationID, mBabyID, state, new ResponseHandler() {

            @Override
            public void onInnovationStart() {
                showProgressDialog(null, null);
            }

            @Override
            public void onInnovationSuccess(JsonElement value) {
                int success = value.getAsInt();
                if (success == 1) {
                    Intent returnIntent = new Intent();
                    setResult(RESULT_OK, returnIntent);
                    finish();
                }
            }

            @Override
            public void onChZhRJFailure(String value) {
                tipShow(value);
                Intent returnIntent = new Intent();
                setResult(RESULT_OK, returnIntent);
                finish();
            }

            @Override
            public void onInnovationFinish() {
                dismissProgressDialog();
            }

            @Override
            public void onInnovationExceptionFinish() {
                dismissProgressDialog();
            }
        }, networkUnavailableSampleImpl());
    }

    // 配置图片加载及显示选项
    private void setOptions() {
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_head_unknown)    //在ImageView加载过程中显示图片
                .showImageForEmptyUri(R.drawable.ic_head_unknown)  //image连接地址为空时
                .showImageOnFail(R.drawable.ic_head_unknown)  //image加载失败
                        // .cacheInMemory(true)  //加载图片时会在内存中加载缓存
                        // .cacheOnDisk(true)   //加载图片时会在磁盘中加载缓存
                .build();
    }
}
